package com.neusoft.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.mapper.BooksMapperCustom;
import com.neusoft.model.BooksModel;
import com.neusoft.model.entity.Appointment;
import com.neusoft.model.entity.Books;
import com.neusoft.model.entity.BooksExample;
import com.neusoft.model.vo.BooksModelVO;
import com.neusoft.service.AppointmentService;
import com.neusoft.service.BooksService;
import com.neusoft.util.PaginationUtil;

@Service
public class BooksServiceImpl implements BooksService{

	@Autowired
	private BooksMapperCustom booksMapper;
	@Autowired
	private AppointmentService appointmentService;

	
	@Override
	public Books findBooksByID(BooksModelVO booksModelVO){
		Books books=	 booksMapper.selectByPrimaryKey(booksModelVO.getBooksModel().getId());
		return  books;
	}
	
	@Override
	public BooksModel findBooksByCallno(BooksModelVO booksModelVO) {
		
		BooksExample be =new BooksExample();
		BooksExample.Criteria bec=be.createCriteria();
		BooksModel booksModel=new BooksModel();
		bec.andCallnoLike(booksModelVO.getBooksModel().getCallno());
		booksModel.setRows(booksMapper.selectByExample(be));
		return booksModel;
	}
	
	@Override
	public BooksModel findBooksByPublish(BooksModelVO booksModelVO) {
		BooksExample be =new BooksExample();
		BooksExample.Criteria bec=be.createCriteria();
		BooksModel booksModel=new BooksModel();
		bec.andPublishLike(booksModelVO.getBooksModel().getPublish());
		booksModel.setRows(booksMapper.selectByExample(be));
		return booksModel;
	}
	
	@Override
	public BooksModel findBooksByISBN(BooksModelVO booksModelVO) {
		BooksExample be =new BooksExample();
		BooksExample.Criteria bec=be.createCriteria();
		BooksModel booksModel=new BooksModel();
		bec.andIsbnEqualTo(booksModelVO.getBooksModel().getIsbn());
		booksModel.setRows(booksMapper.selectByExample(be));
	    
		return booksModel;
	}

	@Override
	public BooksModel findBooksByBookName(BooksModelVO booksModelVO) {
		BooksExample be =new BooksExample();
		BooksExample.Criteria bec=be.createCriteria();
		BooksModel booksModel=new BooksModel();
		bec.andBookNameLike(booksModelVO.getBooksModel().getBookName());
		booksModel.setRows(booksMapper.selectByExample(be));
	    
		return booksModel;
	}

	@Override
	public BooksModel findBooksByAuthor(BooksModelVO booksModelVO) {
		BooksExample be =new BooksExample();
		BooksExample.Criteria bec=be.createCriteria();
		BooksModel booksModel=new BooksModel();
		bec.andAuthorLike(booksModelVO.getBooksModel().getAuthor());
		booksModel.setRows(booksMapper.selectByExample(be));
		return booksModel;
	}

	@Override
	public BooksModel findBooksByTag(BooksModelVO booksModelVO) {
		BooksExample be =new BooksExample();
		BooksExample.Criteria bec=be.createCriteria();
		BooksModel booksModel=new BooksModel();
		bec.andTagEqualTo(booksModelVO.getBooksModel().getTag());
		booksModel.setRows(booksMapper.selectByExample(be));
		return booksModel;
		
	}

	@Override
	public BooksModel findBooksByVo(BooksModelVO booksModelVO) {
		BooksExample be =new BooksExample();
		BooksExample.Criteria bec=be.createCriteria();
		BooksModel booksModel=new BooksModel();
		String bookname=booksModelVO.getBooksModel().getBookName();
		
		
		if(bookname!=null&&!bookname.equals("")){
			StringBuffer sb=new StringBuffer();
			sb.append('%');
			sb.append(bookname);
			sb.append('%');
	     	bec.andBookNameLike(sb.toString());
		}
		String author=booksModelVO.getBooksModel().getAuthor();
		if(author!=null&&!author.equals("")){
			bec.andAuthorEqualTo(author);
		}
		String tag=booksModelVO.getBooksModel().getTag();
		if(tag!=null&&!tag.equals("")){
			bec.andTagEqualTo(tag);
		}
		
		String isbn = booksModelVO.getBooksModel().getIsbn();
		
		if(isbn!=null&&!isbn.equals("")){
			bec.andIsbnEqualTo(isbn);
		}
		
String publish = booksModelVO.getBooksModel().getPublish();
		
		if(publish!=null&&!publish.equals("")){
			bec.andPublishEqualTo(publish);
		}
		
String callno = booksModelVO.getBooksModel().getCallno();
		
		if(callno!=null&&!callno.equals("")){
			bec.andCallnoEqualTo(callno);
		}
		
	    List<Books> booksList=booksMapper.selectByExample(be);
		booksModel.setRows( booksList);
	    
		return booksModel;
	}

	@Override
	public void inseartBook(Books book) {
		
		if(book!=null){
		   booksMapper.insert(book);
		}
	}

	/**
	 * 返回分页查询的结果
	 */
	@Override
	public BooksModel findBooksByVoEU(BooksModelVO booksModelVO, int page,
			int rows) {
		BooksExample be =new BooksExample();
		BooksExample.Criteria bec=be.createCriteria();
		BooksModel booksModel=new BooksModel();
		int begin=(page-1)*rows;
		int totalRecords=countBooks();
		PaginationUtil paginationUtil=new PaginationUtil(begin, rows, totalRecords);
		be.setPagination(paginationUtil);
		String bookname=booksModelVO.getBooksModel().getBookName();
		
		
		if(bookname!=null&&!bookname.equals("")){
			StringBuffer sb=new StringBuffer();
			sb.append('%');
			sb.append(bookname);
			sb.append('%');
	     	bec.andBookNameLike(sb.toString());
		}
		String author=booksModelVO.getBooksModel().getAuthor();
		if(author!=null&&!author.equals("")){
			bec.andAuthorEqualTo(author);
		}
		String tag=booksModelVO.getBooksModel().getTag();
		if(tag!=null&&!tag.equals("")){
			bec.andTagEqualTo(tag);
		}
		
		String isbn = booksModelVO.getBooksModel().getIsbn();
		
		if(isbn!=null&&!isbn.equals("")){
			bec.andIsbnEqualTo(isbn);
		}
		
	    List<Books> booksList=booksMapper.selectByExample(be);
		booksModel.setRows( booksList);
		booksModel.setTotal(totalRecords);
	    
		return booksModel;		
	}

	/**
	 * 返回书籍的总数
	 */
	@Override
	public int countBooks() {
		List<Books> booklist=new ArrayList<Books>();
		BooksExample booksExample=new BooksExample();
		booklist=booksMapper .selectByExample(booksExample);
		return booklist.size();
	}

	@Override
	public List<Books> findBooksByActiveId(BooksModelVO booksModelVO) {

		List<Books> booksliList=booksMapper.findBooksByUserId(booksModelVO.getId());
		
		return booksliList;
	}
	
	
	@Override
	public void updateStatus(BooksModel booksModel){
		for(Books book: booksModel.getRows()){
			
			
			int status=book.getStatus();
		
			int id=book.getId(); 
			Appointment appointment=appointmentService.findAppointmentByBookID(id);
			Date date=new Date();
			if((status==2||status==3)&&appointment!=null&&appointment.getDeadline()!=null&&date.after(appointment.getDeadline())){
//				UpdateStatus updateStatus=new UpdateStatus();
				
				Books books=new Books();
				books.setId(id);
				books.setStatus(3-status);
				 
				booksMapper.updateByPrimaryKeySelective(books);
				appointmentService.deleteAppointmentByBookID(id);
				}
			}
		
		
		
	}
	
	
	@Override
		public List<BooksModel> distinguish(BooksModel booksModels){
			this.updateStatus(booksModels);
			  List<Books> rows=booksModels.getRows();
			List<BooksModel> bookLists=new ArrayList<BooksModel>();
			System.out.println(rows.size()+"t");
			Set<String> set=new HashSet<String>();
			for(Books book:rows){
				set.add(book.getCallno());
				System.out.println(book.getCallno()+"k");
			}
//			System.out.println(set.size()+"f");
			Iterator<String> it = set.iterator();
			while (it.hasNext()) {
				BooksModel booksModel0=new BooksModel();
			  String str = it.next();
			  System.out.println(str);
			  BooksModelVO booksModelVO=new BooksModelVO();
			  BooksModel booksModel=new BooksModel();
			  
			  booksModel.setCallno(str);
//			  booksModel.setAuthor("(奥)托马斯·布热齐纳(Thomas Brezina)");
			  booksModelVO.setBooksModel(booksModel);
			  BooksModel booksModel2=this.findBooksByCallno(booksModelVO);
				int numOfBorrowAbled=0;//可借数
				int numOfAtLibrary=0;//在馆数
				System.out.println(booksModel2.getRows().size());
				booksModel0.setRows(booksModel2.getRows());
			  for (int i=0;i<booksModel2.getRows().size();i++){
				  
				  numOfBorrowAbled++;
				  int status=booksModel2.getRows().get(i).getStatus();
				  if(status==0){
					  numOfAtLibrary++;
				  }
			  }
			  System.out.println(booksModel2.getRows().get(0).getId()+"id");
			  booksModel0.setAuthor(booksModel2.getRows().get(0).getAuthor());
			  booksModel0.setBookName(booksModel2.getRows().get(0).getBookName());
			  booksModel0.setPublish(booksModel2.getRows().get(0).getPublish());
			  booksModel0.setCallno(booksModel2.getRows().get(0).getCallno());
			  booksModel0.setNumOfAtLibrary(numOfAtLibrary);
			  booksModel0.setNumOfBorrowAbled(numOfBorrowAbled);
			  
//			  booksModel0.setStatus(status);
			  bookLists.add(booksModel0);
			}
			
			return bookLists;
		}
	
	@Override
	public void updateStatusToTwo(int id){
 
			
				Books books=new Books();
				books.setId(id);
				books.setStatus(2);
				 
				booksMapper.updateByPrimaryKeySelective(books);
				
			
	}

	@Override
	public void insertBooks(BooksModelVO booksModelVO){
		booksMapper.insert(booksModelVO.getBooksModel());
	}
	
	@Override
	public BooksModel findAllBooks(){
		BooksModel booksmodel= new BooksModel();
		BooksExample bookexample = new BooksExample();
		BooksExample.Criteria bc = bookexample.createCriteria();
		bc.getAllCriteria();
		booksmodel.setRows(booksMapper.selectByExample(bookexample));
		
		return booksmodel;
	}
	
	//已修改
	@Override
	public void updateBooksbyID(Books record){
		booksMapper.updateByPrimaryKeySelective(record);
		
	}
	
	@Override
	public Books findBooksbyID(Integer id){
		BooksModel booksModel = new BooksModel();
		Books book = booksMapper.selectByPrimaryKey(id);
		return book;
	}
	
	@Override
	public void deleteBooksbyID(int bookID) {
		// TODO Auto-generated method stub
		
		  booksMapper.deleteByPrimaryKey(bookID);
		
	}

}
